package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.common.LeaseException;
import com.easychat.common.PageResult;
import com.easychat.common.Result;
import com.easychat.dto.GroupInfoDto;
import com.easychat.dto.TokenUserInfoDto;
import com.easychat.entity.GroupInfo;
import com.easychat.entity.UserContact;
import com.easychat.enums.ExceptionCodeEnum;
import com.easychat.enums.MessageTypeEnum;
import com.easychat.service.GroupInfoService;
import com.easychat.service.UserContactService;
import com.easychat.utils.LoginUserHolder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.rmi.dgc.Lease;
import java.util.List;

/**
 * (GroupInfo)表控制层
 *
 * @author makejava
 * @since 2024-10-08 19:54:12
 */
@RestController
@RequestMapping("groupInfo")
public class GroupInfoController {

    /**
     * 服务对象
     */
    @Resource
    private GroupInfoService groupInfoService;
    @Resource
    private UserContactService userContactService;

    /**
     * 分页查询我的群组
     * @return 查询结果
     */
    @GetMapping("/loadMyGroup")
    public Result<PageResult> queryByPage(@RequestParam Integer sizeNumber , @RequestParam Integer pageNumber) {
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        String userId = tokenUserInfoDto.getUserId();
        PageRequest pageRequest = PageRequest.of(pageNumber, sizeNumber);
        PageResult page = groupInfoService.queryByPage(userId,pageRequest);
        return Result.ok(page);
    }

    @GetMapping("/getGroupInfo")
    public Result getGroupInfo(@NotEmpty String groupId){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        UserContact userContact = userContactService.getUserContactByUserIdAndContactId(tokenUserInfoDto.getUserId(),groupId);
        if(userContact==null ){
            throw new LeaseException("未加入群组或不存在此群");
        }
        //根据groupId和userId查询群组信息，要确保是群成员在查询
        GroupInfo groupInfo = groupInfoService.getGroupInfoByGroupId(groupId,tokenUserInfoDto.getUserId());
        return Result.ok(groupInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<GroupInfo> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.groupInfoService.queryById(id));
    }

    /**
     * 新增群组
     *
     * @param groupInfoDto 实体
     * @return 新增结果
     */
//    @GlobalInterceptor
    @PostMapping("/saveGroup")
    public Result saveGroup( @RequestBody @Valid GroupInfoDto groupInfoDto, MultipartFile avatarFile, MultipartFile avatarCover) throws IOException {

        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        groupInfoService.saveGroup(groupInfoDto,avatarCover,avatarFile);
        return Result.ok();
    }

   @PostMapping("/addOrRemoveGroupUser")
    public Result addOrRemoveGroupUser(@NotEmpty String groupId,
                                       @NotEmpty String selectContacts,
                                       @NotNull Integer opType){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        groupInfoService.addOrRemoveGroupUser(tokenUserInfoDto,groupId,selectContacts,opType);
        return Result.ok();

   }
    @PostMapping("/leaveGroup")
    public Result leaveGroup(@NotEmpty String groupId){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        groupInfoService.leaveGroup(tokenUserInfoDto.getUserId(),groupId, MessageTypeEnum.LEAVE_GROUP);
        return Result.ok();

    }
    @PostMapping("/dissolutionGroup")
    public Result dissolutionGroup(@NotEmpty String groupId){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        groupInfoService.dissolutionGroup(groupId,tokenUserInfoDto.getUserId());
        return Result.ok();
    }

}

